import globalVar as gv

Window = None
ToolBar = None  # 工具条


def _initGlobals():
    global Window
    global ToolBar
    Window = gv.get_value('mainWindow')
    ToolBar = Window.toolBar  # 工具条


def init():
    _initGlobals()   # 初始化全局变量
    win = Window
    toolBar = win.toolBar
    toolList = toolBar.toolList


def selectedLayerChangeEvent(arg):
    """
    :param arg: [current imageLayer, previous imageLayer]
    :return:
    """
    # print(arg)
    tool = ToolBar.currentTool
    print(tool.name, ':图层改变')
    # 禁用前图层所有 graphicsItem
    previous = arg[1]
    if previous:
        previous.graphicsItem.setEnabled(False)
        # 工具取消选择图层事件
        event = getattr(tool, 'toolFocusOffLayerEvent', None)
        if event:
            layer = previous
            if layer:
                print(tool.name, '取消焦距', layer.text())
                event(tool, layer)
    # 启用现图层所有 graphicsItem
    current = arg[0]
    if current:
        current.graphicsItem.setEnabled(True)
        # 工具选择图层事件
        event = getattr(tool, 'toolFocusOnLayerEvent', None)
        if event:
            layer = current
            if layer:
                print(tool.name, '焦距', layer.text())
                event(tool, layer)
